package com.trust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trust.constant.Constants;
import com.trust.entity.SystemCity;
import com.trust.entity.VO.SystemCityTreeVO;
import com.trust.mapper.SystemCityMapper;
import com.trust.service.SystemCityService;
import com.trust.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SystemCityServiceImpl extends ServiceImpl<SystemCityMapper, SystemCity> implements SystemCityService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public List<SystemCity> getProvince() {
        LambdaQueryWrapper<SystemCity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemCity::getParentId, 0);
        wrapper.eq(SystemCity::getLevel, 0);
        return this.list(wrapper);
    }

    @Override
    public Object getListTree() {
        Object cityList = redisUtil.get(Constants.CITY_LIST_TREE);
        if(null == cityList){
            this.setListTreeRedis();
        }
        return redisUtil.get(Constants.CITY_LIST_TREE);
    }

    @Override
    public String getNameById(Integer id) {
        LambdaQueryWrapper<SystemCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemCity::getId, id).eq(SystemCity::getIsShow, 1);
        SystemCity city = this.getOne(lambdaQueryWrapper);
        if (null == city){
            return "";
        }
        return city.getName();
    }

    private void setListTreeRedis(){
        //循环数据，把数据对象变成带list结构的vo
        List<SystemCityTreeVO> treeList = new ArrayList<>();

        LambdaQueryWrapper<SystemCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(SystemCity::getId, SystemCity::getCityId, SystemCity::getParentId, SystemCity::getName);
        lambdaQueryWrapper.eq(SystemCity::getIsShow, true);
        List<SystemCity> allTree = this.list(lambdaQueryWrapper);

        if (allTree == null) {
            return;
        }

        for (SystemCity systemCity : allTree) {
            SystemCityTreeVO systemCityTreeVo = new SystemCityTreeVO();
            BeanUtils.copyProperties(systemCity, systemCityTreeVo);
            systemCityTreeVo.setValue(systemCity.getId());
            systemCityTreeVo.setLabel(systemCity.getName());
            treeList.add(systemCityTreeVo);
        }


        //返回
        Map<Integer, SystemCityTreeVO> map = new HashMap<>();
        //cityId 为 key 存储到map 中
        for (SystemCityTreeVO systemCityTreeVo1 : treeList) {
            map.put(systemCityTreeVo1.getCityId(), systemCityTreeVo1);
        }


        List<SystemCityTreeVO> list = new ArrayList<>();
        for (SystemCityTreeVO tree : treeList) {
            //子集ID返回对象，有则添加。
            SystemCityTreeVO tree1 = map.get(tree.getParentId());
            if (tree1 != null) {
                tree1.getChildren().add(tree);
            } else {
                list.add(tree);
            }
        }
        redisUtil.set(Constants.CITY_LIST_TREE, list);
    }
}
